<%@page import="net.chat.utils.Util"%>
<%@page import="net.chat.User"%>
<%@page import="net.chat.Forum"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="net.chat.server.ChatServer"%>
<%@page import="java.util.ArrayList"%>
<%
            Object user = session.getAttribute(Util.USER_SESS);
            if (user == null) {
                response.sendRedirect("login.jsp?err=2");
            }
%>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
    Prabhat, initial page, CSS and W3C validation
    Prabhat, added redirection login page if no user session and show error msg
             when redirected to this page
    Amir , added Javascript
    Amir , made page dynamic JSP
    Amir , made page long life connection
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
        <title>Net-Chat:Forums</title>
        <link rel="stylesheet" type="text/css" href="../style/common.css"></link>
        <script type="text/javascript" src="../js/ajax-pure.js"></script>
        <script type="text/javascript">
            var rqTimeout = <%= Util.AJAX_LONG_LIVE_TIMEOUT%>; // 5 minutes
            window.onload=function(){
                receive("<%= Util.SERVLET_PATH%>ForumServlet?timeout="+rqTimeout+"&t="+Math.random()/*to avoid get a cached result*/, "forumstable",rqTimeout);
            }
        </script>
    </head>
    <body>
        <div id="container">
            <div id="top">
                <h1>Net-Chat</h1>
            </div>
            <div id="leftnav">
                <p>
                    <%
                                if (user != null) {
                                    User u = (User) user;
                                    out.print("<b>Hello " + u.getFname() + " " + u.getLname() + "!</b>");
                                    out.print(" <a href=\"" + Util.SERVLET_PATH + "UserServlet?action=logout&uid=" + u.getUid() + "\">logout</a>");
                                }
                    %>
                    <br /><br />
                    This page lists all the forums users can choose to join in and chat with the other users in the forums. The page gets updated as new forums are added or old ones becoming inactive.
                </p>
            </div>
            <div id="content">
                <h2>Forums</h2>
                <%
                            String err = request.getParameter("err");
                            if (err != null) {
                                if (err.equals("f")) {
                                    out.println("<span class=\"err\">Redirected from <b>joinforum</b> page. Forum ID required! </span><br/>");
                                } else if (err.equals("r")) {
                                    out.println("<span class=\"err\">Redirected from <b>register</b> page. Not allowed to do this when logged in! </span><br/>");
                                } else if (err.equals("l")) {
                                    out.println("<span class=\"err\">Redirected from <b>login</b> page. Already logged in! </span><br/>");
                                } else if (err.equals("i")) {
                                    out.println("<span class=\"err\">Redirected from <b>joinchat</b> page. You have been inactive for more than " + Util.MAX_INACTIVE_MIN + " mins and thus removed from forum! </span><br/>");
                                }
                            }
                %>
                <p>
                    <a href="createforum.jsp">create new forum</a>
                </p>

                <div id="forumstable">
                    <%
                                ChatServer server = ChatServer.getInstance();
                                List<Forum> forums;
                                forums = server.getForums();
                                out.println("");
                                out.println("<table class=\"forumstab\">");
                                out.println("<tr>");
                                out.println("<th>#</th>");
                                out.println("<th>forum id</th>");
                                out.println("<th>name</th>");
                                out.println("<th>category</th>");
                                out.println("<th>users</th>");
                                out.println("<th>join</th>");
                                out.println("</tr>");

                                if (forums.size() > 0) {
                                    // send forums to client
                                    int i = 0;
                                    String className = "oddtr";//className="oddtr"|"eventr"
                                    for (Iterator<Forum> itr = forums.iterator(); itr.hasNext();) {
                                        ++i;
                                        if (className.equals("eventr")) {
                                            className = "oddtr";
                                        } else {
                                            className = "eventr";
                                        }
                                        Forum forum = itr.next();
                                        out.println("<tr class=\"" + className + "\" id=\"forumno" + Integer.toString(forum.getFid()) + "\">");
                                        out.println("<td>" + i + "</td>");
                                        out.println("<td>" + forum.getFid() + "</td>");
                                        out.println("<td>" + forum.getName() + "</td>");
                                        out.println("<td>" + forum.getCategory() + "</td>");
                                        out.println("<td>" + forum.getUsers().size() + "</td>");
                                        out.println("<td><a href='" + "joinforum.jsp?fid=" + forum.getFid() + "'>join</a></td>");
                                        out.println("</tr>");
                                    }
                                }
                                out.println("</table>");
                                out.println("<br /><b>" + forums.size() + " forums!</b>");
                    %>
                </div>

            </div>
            <div id="messages"></div>
            <div id="footer">
                <div class="alignleft">Author: Totoo, Ghaffari</div>
                <div class="alignright">Network Applications</div>
                <div class="clr"></div>
            </div>
        </div>
        <p class="center">
            <a href="http://validator.w3.org/check?uri=referer">
                <img class="w3img" src="../img/valid-xhtml10-blue.png" alt="Valid XHTML 1.0 Strict" />
            </a>
            <a href="http://jigsaw.w3.org/css-validator/check/referer">
                <img class="w3img" src="../img/vcss-blue.gif" alt="Valid CSS!" />
            </a>
        </p>
    </body>
</html>